package com.neudu.yiyang.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neudu.yiyang.commons.JSONReturn;
import com.neudu.yiyang.pojo.Food;
import com.neudu.yiyang.pojo.Meal;
import com.neudu.yiyang.service.FoodService;
import com.neudu.yiyang.service.MealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
public class MealController {

    @Autowired
    JSONReturn jsonReturn;

    @Autowired
    MealService mealService;

    @Autowired
    FoodService foodService;

    @RequestMapping("get_all_meals")
    public String getAll(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<Meal> list = mealService.list();
            PageInfo<Meal> pageInfoReturn = new PageInfo<>(list);
            return jsonReturn.returnSuccess(pageInfoReturn);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("get_all_foods")
    public String getAllFoods(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<Food> list = foodService.list();
            PageInfo<Food> pageInfoReturn = new PageInfo<>(list);
            return jsonReturn.returnSuccess(pageInfoReturn);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @PostMapping("add_one_meal")
    public String addOne(@RequestBody Meal meal) {
        try {
            meal.setIsDeleted(0);
            mealService.save(meal);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @PostMapping("add_food_to_meal")
    public String addFood(@RequestBody Food food) {
        try {
            foodService.save(food);
            return jsonReturn.returnSuccess(Map.of("foodId", food.getId()));
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("get_foods")
    public String getFoods() {
        try {
            List<Food> foods = foodService.list();
            return jsonReturn.returnSuccess(foods);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("get_food_by_id")
    public String getFoodById(@RequestParam("id") Integer id) {
        try {
            Food food = foodService.getById(id);
            if (food != null) {
                return jsonReturn.returnSuccess(food);
            } else {
                return jsonReturn.returnError("Food not found");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("modify_food_by_id")
    public String modifyFoodById(@RequestBody Food food) {
        try {
            foodService.updateById(food);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("remove_food_by_id")
    public String removeFoodById(@RequestParam("id") Integer id) {
        try {
            foodService.removeById(id);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }
}